package com.ufo.store.management.widget.zxing.encode;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.text.TextUtils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.ufo.store.management.R;

import java.util.Hashtable;

/**
 * Created by taoli on 2016/8/22.
 */
public class EncodingHandler {
    private static final int BLACK = 0xff000000;
    private static final int WHITE = 0xffffffff;

    public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix matrix = new MultiFormatWriter().encode(str,
                BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = BLACK;
                } else { // 无信息设置像素点为白色
                    pixels[y * width + x] = WHITE;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    public static Bitmap createQRCodeWithLogo(Context context, String str,
                                              int widthAndHeight) throws WriterException {
        QRCodeWriter writer = new QRCodeWriter();
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new QRCodeWriter().encode(str,
                BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);
        int[] pixels = new int[widthAndHeight * widthAndHeight];

        Bitmap[] bitmaps = new Bitmap[2];
        bitmaps[1] = BitmapFactory.decodeResource(context.getResources(),
                R.mipmap.ic_launcher);// logo图标
        int imageW = bitmaps[1].getWidth();
        int imageH = bitmaps[1].getHeight();
        int startW = widthAndHeight / 2 - imageW / 2;
        int starH = widthAndHeight / 2 - imageH / 2;
        for (int y = 0; y < widthAndHeight; y++) {
            for (int x = 0; x < widthAndHeight; x++) {
                if ((x <= startW || x >= starH + imageW)
                        || (y <= starH || y >= +imageH)) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * widthAndHeight + x] = BLACK;
                    }
                } else {
                }
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(widthAndHeight, widthAndHeight,
                Bitmap.Config.ARGB_8888);

        bitmap.setPixels(pixels, 0, widthAndHeight, 0, 0, widthAndHeight, widthAndHeight);

        bitmaps[0] = bitmap;

        Bitmap bm = combineBitmaps(bitmaps, startW, starH);
        return bm;
    }

    private static Bitmap combineBitmaps(Bitmap[] bitmaps, int w, int h) {// 添加logo
        Bitmap newBitmap = Bitmap.createBitmap(bitmaps[0].getWidth(),
                bitmaps[0].getHeight(), Bitmap.Config.ARGB_8888);
        Canvas cv = new Canvas(newBitmap);

        for (int i = 0; i < bitmaps.length; i++) {
            if (i == 0) {
                cv.drawBitmap(bitmaps[0], 0, 0, null);
            } else {
                cv.drawBitmap(bitmaps[i], w, h, null);
            }
            cv.save();
            cv.restore();
        }
        return newBitmap;
    }

    public static Bitmap gerateLogoCode(Context context, String str,
                                        int widthAndHeight) throws WriterException {
        if (!TextUtils.isEmpty(str)) {
            //根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（600*600）
            Bitmap qrCodeBitmap = createQRCode(str, widthAndHeight);

            //------------------添加logo部分------------------//
            Bitmap logoBmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);

            //二维码和logo合并
            Bitmap bitmap = Bitmap.createBitmap(qrCodeBitmap.getWidth(), qrCodeBitmap
                    .getHeight(), qrCodeBitmap.getConfig());
            Canvas canvas = new Canvas(bitmap);
            //二维码
            canvas.drawBitmap(qrCodeBitmap, 0, 0, null);
            //logo绘制在二维码中央
            canvas.drawBitmap(logoBmp, qrCodeBitmap.getWidth() / 2
                    - logoBmp.getWidth() / 2, qrCodeBitmap.getHeight()
                    / 2 - logoBmp.getHeight() / 2, null);
            return bitmap;
        }
        return null;
    }
}
